package com.whzykj.ctrl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.whzykj.common.CommonUtils;
import com.whzykj.common.DateUtil;
import com.whzykj.common.DictCache;
import com.whzykj.model.GoodsInfo;
import com.whzykj.model.Price;
import com.whzykj.model.RechargeActivity;
import com.whzykj.model.RechargeBill;
import com.whzykj.model.Station;
import com.whzykj.model.Userinfo;

public class ActivityController extends Controller {
	
	public void index() {
		this.renderJsp("/admin/system/activity/activity.jsp");
	}
	
	/****
	 * 活动列表
	 */
	public void activitylist() {
		Integer page = this.getParaToInt("page");
		Integer rows = this.getParaToInt("rows");

		String starttime = this.getPara("start_time");
		String endtime = this.getPara("end_time");
		
		String sqlBefore = " select *   ";
		StringBuffer sqlAfter = new StringBuffer(" FROM Recharge_Activity r where 1=1   ");
		
		if (starttime != null && !"".equals(starttime)) {
			sqlAfter.append(" and r.start_time >= '" + starttime +" 00:00:00' ");
		}
		
		if(endtime != null && !"".equals(endtime)){
			sqlAfter.append(" and r.end_time <= '" + endtime + " 59:59:59' ");
		}
		
		sqlAfter.append("  ORDER BY r.id DESC ");
		
		Page<Record> paginate = Db.paginate(page, rows, sqlBefore,sqlAfter.toString());
		List<Record> custlist = paginate.getList();
		if (custlist != null && custlist.size() > 0) {
			for (Record rd  : custlist) {
				
				String activity_state = rd.getStr("activity_state");
				String activity_statename = DictCache.cache.getDicByText(String.valueOf(activity_state), "ACTIVITY_STATE");
				rd.set("activity_statename", activity_statename);
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", custlist);
		map.put("total", paginate.getTotalRow());
		this.renderJson(map);
	}
	
	/****
	 * 活动新增
	 */
	public void activityadd() {
		
		String activity_name = getPara("activity_name");
		String p_start_time = getPara("p_start_time");
		String p_end_time = getPara("p_end_time");
		
		RechargeActivity activity = new RechargeActivity();
		activity.set("activity_name", activity_name);
		activity.set("start_time", p_start_time);
		activity.set("end_time", p_end_time);
		activity.set("activity_state", "01");//正常
		boolean save = activity.save();
		this.renderText(save ? "0" : "1");
	}
	
	public void activityview() {
		String id = getPara("id");
		RechargeActivity ra = RechargeActivity.dao.findById(id);
		this.renderJson(ra);
	}
	
	public void activityedit(){
		
		String id = getPara("id");
		RechargeActivity ra = RechargeActivity.dao.findById(id);
		
		String activity_name = getPara("activity_name");
		String p_start_time = getPara("p_start_time");
		String p_end_time = getPara("p_end_time");
		
		ra.set("activity_name", activity_name);
		ra.set("start_time", p_start_time);
		ra.set("end_time", p_end_time);
		boolean save = ra.update();
		this.renderText(save ? "0" : "1");
	}
	
	public void activitystate() {
		String id = this.getPara("id");
		String activity_state = this.getPara("activity_state");
		RechargeActivity ra = RechargeActivity.dao.findById(id);
		ra.set("activity_state", activity_state);
		boolean update = false;
		update = ra.update();
		this.renderText(update ? "1" : "0");
	}
	
}
